
/**
* @author zxc
*/
tm.admin.content =
{
    timers: [],

    init: function()
    {
        $('.delete-image').click(tm.admin.content.confirmDeleteImageClick);
        $('.delete-activity').click(tm.admin.content.confirmDeleteActivityClick);
        $('.clear-content').click(tm.admin.content.confirmClearContentClick);
        $('.add-images').click(tm.admin.content.addMoreImagesClick);
        $('.previw-link').click(tm.admin.content.editOrPreviewTextClick);
        $('.content-text').keyup(tm.admin.content.textareaKeyUp).each(function() {
            tm.admin.content.textareaKeyUpOnTimer(this);
        });
    },

    confirmDeleteImageClick: function()
    {
        return confirm('Are you sure you want to delete image?');
    },

    confirmClearContentClick: function()
    {
        return confirm('Are you sure you want to delete content?');
    },

    confirmDeleteActivityClick: function()
    {
        return confirm('Are you sure you want to delete activity?');
    },

    addMoreImagesClick: function()
    {
        var table = $(this).parent().prev();
        var number = table.find('tr').size();
        var inputs = table.find('input');

        table.find('tr:last').after('<tr><td width="150">Image ' + (++number) +  ':</td>' +
        '<td><input type="file" name="' + inputs[0].name + '" /> Title: <input size="50" type="text" name="' + inputs[1].name + '" value="" /></td>' +
        '</tr><tr><td width="150">Image ' + (++number) +  ':</td>' +
        '<td><input type="file" name="' + inputs[0].name + '" /> Title: <input size="50" type="text" name="' + inputs[1].name + '" value="" /></td>' +
        '</tr><tr><td width="150">Image ' + (++number) +  ':</td>' +
        '<td><input type="file" name="' + inputs[0].name + '" /> Title: <input size="50" type="text" name="' + inputs[1].name + '" value="" /></td></tr>');

        return false;
    },

    editOrPreviewTextClick: function()
    {
        var td = $(this).parent().prev();
        if(td.children('div.html-container').length == 0){
            var textarea = td.children()[0];        
            td.html('<div class="html-container" style="width:500px;">' + textarea.value + '</div>');
        }
        else{
            var div = td.children()[0];
            var name = td.next().children()[0].name;
            td.html('<textarea onFocus="this.style.height=\'300px\'" onBlur="this.style.height=\'50px\'" class="content-text" style="height:50px; width:500px;" name="' + name + '">' + div.innerHTML + '</textarea>')
        }
        return true;
    },

    textareaKeyUp: function()
    {
        var control = this;
        var hash = this.name + this.offsetTop + this.offsetLeft;

        if (tm.admin.content.timers[hash] !== null && tm.admin.content.timers[hash] !== undefined) {
            window.clearTimeout(tm.admin.content.timers[hash]);
            tm.admin.content.timers[hash] = null
        }

        tm.admin.content.timers[hash] = window.setTimeout(function() {
            tm.admin.content.textareaKeyUpOnTimer(control);
        }, 1000);
    },

    textareaKeyUpOnTimer: function(control)
    {
        control = $(control);

        if (! control) {
            return;
        }

        var wordsCounter = (control.val()).countWords(true);

        if (control.next('div.indicator').length == 0) {
            control.after('<div class="indicator"></div>');
        }

        control.next('div.indicator').html('<small>' + ((wordsCounter == 1) ? ('LBL_WORDS_QUANTITY_S'.translate()) : ('LBL_WORDS_QUANTITY_M'.translate( {s: wordsCounter} ))) + '</small>').css( { 'color': '#998e85' } );
    }
}

$(tm.admin.content.init);
